<template>
  <div class="login-root">
    <!-- 左侧插画区域 -->
    <div class="illustration-section">
      <div class="bg-circle"></div>
      <div class="bg-circle"></div>
      <div class="bg-circle"></div>
      <div class="illustration-container">
        <!-- 主插画SVG -->
        <img class="illustration" src="/src/assets/svg/illustration.svg" alt="illustration" />
        <img class="rocket" src="/src/assets/svg/rocket.svg" alt="rocket" />
        <!-- 数据点动画 -->
        <div class="data-point"></div>
        <div class="data-point"></div>
        <div class="data-point"></div>
        <div class="data-point"></div>
      </div>
      <h2 class="illustration-title">欢迎使用岚迹后台管理系统</h2>
      <p class="illustration-subtitle">高效、安全、可靠的企业级管理平台</p>
    </div>
    <!-- 右侧登录区域 -->
    <div class="login-section">
      <div class="login-header">
        <h1 class="login-title">用户登录</h1>
        <p class="login-subtitle">请输入您的账号和密码</p>
      </div>
      <el-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="loginRules"
          label-position="top"
          size="large"
          @keyup.enter="handleLogin"
          class="login-el-form"
      >
        <el-form-item prop="username">
          <el-input
              v-model="loginForm.username"
              placeholder="请输入用户名"
              :prefix-icon="User"
              class="form-input"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
              v-model="loginForm.password"
              placeholder="请输入密码"
              type="password"
              show-password
              :prefix-icon="Lock"
              class="form-input"
          />
        </el-form-item>
        <div class="remember-forgot">
          <el-checkbox v-model="loginForm.remember">记住我</el-checkbox>
          <router-link to="/forgot-password" class="forgot-password">忘记密码?</router-link>
        </div>
        <el-form-item>
          <el-button
              type="primary"
              class="login-button"
              :loading="loading"
              @click="handleLogin"
          >
            登 录
          </el-button>
        </el-form-item>
      </el-form>
      <div class="login-footer">
        还没有账号?<router-link to="/register" class="register-link">立即注册</router-link>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import type { FormInstance } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/user.ts'

const router = useRouter()
const userStore = useUserStore()
const loading = ref(false)
const loginFormRef = ref<FormInstance>()

const loginForm = reactive({
  username: 'admin',
  password: '123456',
  remember: false
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度应在3到20个字符之间', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度应在6到20个字符之间', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return
  await loginFormRef.value.validate(async (valid, fields) => {
    if (valid) {
      loading.value = true
      try {
        await userStore.login(loginForm.username, loginForm.password)
        ElMessage.success('登录成功')
        router.push('/admin/index')
      } catch (error) {
        ElMessage.error('登录失败，请检查用户名和密码')
        console.error(error)
      } finally {
        loading.value = false
      }
    } else {
      console.log('验证失败', fields)
    }
  })
}
</script>

<style lang="scss" scoped>

</style>
